<%@page import="models.User"%>
<%@include file="template/configuration.jsp" %>
<%@page import="models.Category"%>
<%@page import="java.util.List"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>${website} || My courses</title>
        ${stylesheet}
        ${jquery}
        ${jstooltip}
        ${jscollapse}
        ${jspagination}
    </head>
    <body>
        <div class="container">
            <%@include file="template/header.jsp" %>
            <div class="content">
                <h1>${uq_my_courses}</h1>
                <!-- clear //-->
                <div class="clear margin-bottom-10"></div>
                <c:set var="first" value="true" />
                <c:choose>
                    <c:when test="${fn:length(categories)!=0}">
                        <c:forEach items="${categories}" var="category">
                            <c:set var="catid" value="${category.id}" /> 
                                    <%
                                    // hu(mc), cd(mc), mc = manage categories
                                    HibernateUtil humc = new HibernateUtil();
                                    CourseDao cdmc = new CourseDao(humc.openSession());
                                    HttpSession sessie = request.getSession();
                                    List<Course> courseListMC = null;
                                    if (sessie.getAttribute("userrole").equals("Administrator")) {
                                        courseListMC = cdmc.findAllByCategory(Long.parseLong(pageContext.getAttribute("catid").toString()), 15);
                                    } 
                                    if (sessie.getAttribute("userrole").equals("Teacher")){
                                        User user = (User) sessie.getAttribute("user");
                                        courseListMC = cdmc.findAllByTeacherAndCategory(user.getMember_id(), Long.parseLong(pageContext.getAttribute("catid").toString()));
                                    }
                                    if (!sessie.getAttribute("userrole").equals("Teacher") && !sessie.getAttribute("userrole").equals("Administrator")){
                                        User user = (User) sessie.getAttribute("user");
                                        
                                        courseListMC = cdmc.findAllByStudentAndCategory(user.getMember_id(), Long.parseLong(pageContext.getAttribute("catid").toString()));
                                    }
                                    request.setAttribute("courses", courseListMC);           
                                    if (courseListMC.size() > 0 || sessie.getAttribute("userrole").equals("Administrator")) {                                         
                                    %>                                                                
                                <div class="bundle" <c:if test="${first != 'true'}">style="margin-top: -50px;"</c:if>>
                                    <c:set var="first" value="false" />
                                    <div class="top">
                                        <div class="left">

                                        </div>
                                        <div class="center">
                                            <h5>${category.name}</h5>
                                        </div>
                                        <div class="right">
                                        </div>
                                    </div>
                                    <!-- clear //-->
                                    <div class="clear"></div>
                                    <div class="full-block" style="min-height: 26px;">
                                        <div style="display: none;">
                                            <c:forEach items="${courses}" var="course">
                                                <fieldset class="inside-block">
                                                    <legend align="center">
                                                        <div class="button no-link"><a>${course.name}</a></div>
                                                    </legend>
                                                    <div class="left">
                                                        <img src="${url}images/course-default.png" />
                                                    </div>
                                                    <div class="right">
                                                        <h4>${uq_description}</h4>
                                                        ${course.description}
                                                    </div>
                                                    <div class="bottom">
                                                        <div class="button float-right"><a href="${url}Course?courseid=${course.id}">${uq_view_course}</a></div>
                                                                                                                                                 
                                                    </div>
                                                </fieldset>
                                                <div class="clear margin-bottom-20"></div>
                                            </c:forEach>
                                        </div>
                                    </div>
                                    <!-- clear //-->
                                    <div class="clear margin-bottom-10" style="display: none;"></div>
                                </div>
                            <% } %>
                        </c:forEach>
                    </c:when>
                    <c:otherwise>
                        <div class="full-block">
                            ${uq_there_are_no_categories_added_yet}
                        </div>
                    </c:otherwise>
                </c:choose>
            </div>
            <%@include file="template/footer.jsp" %>
        </div>
    </body>
</html>
